﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NLite.Data;
using System.Linq;


using DevExpress.XtraBars;

namespace NorthWind
{
    public partial class NortWindControl : DevExpress.XtraEditors.XtraUserControl
    {
        public event EventHandler SelectedRowChanged;

        public List<Customers> CustomersList = new List<Customers>();


        private td.Core.Controls.tdGridView<Customers> gridView = new td.Core.Controls.tdGridView<Customers>();

        public NortWindControl()
        {
            InitializeComponent();
            this.panelControl2.Controls.Add(this.gridView);
            this.gridView.Dock = DockStyle.Fill;
            this.gridView.DisplayColumns = "CompanyName,ContactTitle,ContactName,Address,City,Region,PostalCode,Country,Phone,Fax";
            this.gridView.AddColumnAlias("CompanyName", "公司名");
            this.gridView.AddColumnAlias("ContactName", "联系人");
            this.gridView.AddColumnAlias("ContactTitle", "联系标题");
            this.gridView.AddColumnAlias("Address", "地址");
            this.gridView.AddColumnAlias("City", "城市");
            this.gridView.AddColumnAlias("Region", "区域");

            var db = DbHelper.dbConfiguration;
            using (var ctx = db.CreateDbContext())
            {
                var q = ctx.Set<Customers>();
                CustomersList = q.ToList<Customers>();                
            }
            this.gridView.DataSource = CustomersList.OrderBy(p => p.CustomerID).AsQueryable<Customers>(); ;
            this.gridView.gridView1.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(gridView1_RowCellClick);
            this.gridView.gridView1.OptionsBehavior.ReadOnly = true;
            this.gridView.gridView1.OptionsBehavior.Editable = false;


        }


        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
           
            if (e.RowHandle < this.gridView.gridView1.DataRowCount)
            {
                if (this.SelectedRowChanged != null)
                {

                    this.SelectedRowChanged( RowToCustomers(e.RowHandle), EventArgs.Empty);
                }
            }
        }

        private Customers RowToCustomers(int rowHandle)
        {
            Customers c = new Customers();
            try
            {
                
                c.Address = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "Address");
                c.City = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "City");
                c.CompanyName = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "CompanyName");
                c.ContactName = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "ContactName");
                c.ContactTitle = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "ContactTitle");
                c.Country = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "Country");
               // c.CustomerID = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "CustomerID");
                c.Fax = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "Fax");
                c.Phone = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "Phone");
                c.PostalCode = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "PostalCode");
                c.Region = this.gridView.gridView1.GetRowCellDisplayText(rowHandle, "Region");
                return c;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
            return null;
        }
        private void GetCustomersByFilterString()
        {

            var db = DbHelper.dbConfiguration;
            using (var ctx = db.CreateDbContext())
            {
                var q = ctx.Set<Customers>();
                CustomersList = q.Where(p => p.CustomerID.Contains(this.tbid.Text) &&
                    (p.CompanyName.Contains(this.tbCompany.Text)) &&
                    (p.ContactName.Contains(tbContactName.Text)) &&
                    (p.ContactTitle.Contains(tbContactTitle.Text))).ToList<Customers>();
                
            }
            this.gridView.DataSource = CustomersList  .AsQueryable<Customers>().OrderBy(p=>p.CustomerID);         
        }
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            GetCustomersByFilterString();
        }


    }
}
